package cn.joeg.app.kernel.auth;

import cn.joeg.app.GCore;
import cn.joeg.app.css.util.CSS;
import cn.joeg.app.dto.OptInfo;
import cn.joeg.app.kernel.service.LoginService;
import com.ecfront.dew.common.JsonHelper;
import com.ecfront.dew.common.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

@RestController
@Api(value = "登录", description = "")
@RequestMapping(value = "")
public class AuthAdaptationController {

    private static final Logger logger = LoggerFactory.getLogger(AuthAdaptationController.class);

    @Autowired
    private LoginService loginService;

    @PostMapping(value = "/public/auth/login")
    @ApiOperation(value = "登录", notes = "登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginReq", value = "登录请求", paramType = "body", dataType = "LoginReq", required = true),
    })
    public Resp<OptInfo> login(@RequestBody LoginReq loginReq) {
        logger.info("auth login req: {}", JsonHelper.toJson(loginReq));
        if (loginReq.getLoginId() == null || loginReq.getLoginId().isEmpty() || loginReq.getPassword() == null || loginReq.getPassword().isEmpty()) {
            return Resp.badRequest("请输入登录名和密码");
        }
        // 数据库查询验证密码
        LoginService.Input loginInput = new LoginService.Input();
        loginInput.setLoginId(loginReq.getLoginId());
        loginInput.setPassword(loginReq.getPassword());
        Resp loginResp = loginService.execute(loginInput);
        if (!loginResp.ok()) {
            return Resp.customFail(loginResp.getCode(), loginResp.getMessage());
        }
        OptInfo optInfo = new OptInfo();
        optInfo.setToken(GCore.Util.createUUID());
        optInfo.setAccountCode(loginReq.getLoginId());
        optInfo.setLastLoginTime(new Date());
        optInfo.setRoles(new ArrayList<OptInfo.RoleInfo>() {{
            OptInfo.RoleInfo roleInfo = new OptInfo.RoleInfo();
            roleInfo.setCode("cs");
            roleInfo.setName("玩家");
            roleInfo.setTenantCode("css");
            add(roleInfo);
        }});
//        optInfo.setExt(JsonHelper.toJsonString(null));
        CacheManager.addToken(optInfo);
        logger.info("auth login success: {}", JsonHelper.toJson(optInfo));
        return Resp.success(optInfo);
    }


    @GetMapping(value = "/auth/logininfo")
    @ApiOperation(value = "获取当前登录信息")
    public Resp<OptInfo> getLoginInfo() {
        return Resp.success(GCore.context().optInfo().get());
    }


    @DeleteMapping(value = "/adaptation/auth/logout")
    @ApiOperation(value = "注销", notes = "客服支撑自己的注销接口（不走门户单点）")
    public Resp<Void> logout() {
        // TODO 调用门户注销接口
//        dataDriveAdapterService.logoutByOptUser();
        CSS.Call.deleteCallSession();
        CacheManager.removeToken(GCore.context().optInfo().get().getToken());
        return Resp.success(null);
    }

}
